<template>
  <div class="bg-light">
    <!-- 头部 -->
    <div class="header d-flex align-items-center">
      <router-link to="/politics" class="text-dark">
        <i class="bi bi-chevron-left fs-4"></i>
      </router-link>
      <h5 class="mb-0 mx-auto">AI智能练习</h5>
    </div>

    <!-- 选择题型 -->
    <div 
      v-for="type in exerciseTypes"
      :key="type.id"
      class="exercise-type-card"
      :class="{ selected: selectedType === type.id }"
      @click="selectExerciseType(type)"
    >
      <div class="d-flex justify-content-between align-items-center">
        <div>
          <h5 class="mb-2">{{ type.title }}</h5>
          <p class="text-muted mb-0">{{ type.description }}</p>
        </div>
        <i :class="type.icon + ' fs-3'"></i>
      </div>
    </div>

    <!-- 生成按钮 -->
    <button 
      class="generate-btn" 
      @click="generateExercise"
      :disabled="!selectedType"
    >
      开始生成
    </button>

    <!-- 加载遮罩 -->
    <div class="loading-overlay" :style="{ display: isLoading ? 'flex' : 'none' }">
      <div class="loading-content">
        <div class="spinner-border text-primary spinner" role="status"></div>
        <div>正在分析知识点，生成题目中...</div>
        <small class="text-muted">预计需要10秒</small>
      </div>
    </div>
  </div>
</template>

<script>
import http from '@/router/http';

export default {
  name: 'PoliticsAIView',
  data() {
    return {
      selectedType: null,
      isLoading: false,
      exerciseTypes: [
        {
          id: 'similar',
          title: '相似题推荐',
          description: '基于错题智能推荐相似的练习题',
          icon: 'bi bi-lightbulb'
        },
        {
          id: 'weak',
          title: '薄弱点练习',
          description: '针对性练习易错知识点',
          icon: 'bi bi-graph-up'
        }
      ]
    }
  },
  methods: {
    showToast(message, type = 'danger') {
      const toastContainer = document.createElement('div');
      toastContainer.className = 'toast-container position-fixed top-0 end-0 p-3';
      toastContainer.style.zIndex = '1500';
      
      const toastHtml = `
        <div class="toast show align-items-center text-white bg-${type} border-0" role="alert" aria-live="assertive" aria-atomic="true">
          <div class="d-flex">
            <div class="toast-body">
              ${message}
            </div>
            <button type="button" class="btn-close btn-close-white me-2 m-auto" data-bs-dismiss="toast" aria-label="Close"></button>
          </div>
        </div>
      `;
      
      toastContainer.innerHTML = toastHtml;
      document.body.appendChild(toastContainer);
      
      setTimeout(() => {
        document.body.removeChild(toastContainer);
      }, 3000);
    },
    
    selectExerciseType(type) {
      this.selectedType = type.id
    },
    async generateExercise() {
      this.isLoading = true
      
      try {
        const response = await http.post('/student/ai/politics/practice', {
          type: this.selectedType,
          count: 10,
          difficulty: 'medium'
        })

        if (response.data && response.data.code === 1) {
          const paperId = response.data.response.paperId;
          
          this.$router.push({
            path: '/politics/ai/practice',
            query: { paperId }
          });
        } else {
          this.showToast(response.data.message || '生成练习失败')
        }
      } catch (error) {
        console.error('生成练习失败:', error)
        this.showToast('生成练习失败，请稍后重试')
      } finally {
        this.isLoading = false
      }
    }
  }
}
</script>

<style scoped>
.header {
  padding: 15px;
  background: white;
  position: sticky;
  top: 0;
  z-index: 1000;
  box-shadow: 0 2px 4px rgba(0,0,0,0.05);
}

.exercise-type-card {
  background: white;
  border-radius: 15px;
  padding: 20px;
  margin: 15px;
  cursor: pointer;
  transition: all 0.3s ease;
}

.exercise-type-card:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(0,0,0,0.1);
}

.exercise-type-card.selected {
  border: 2px solid #B19CD9;
}

.generate-btn {
  position: fixed;
  bottom: 20px;
  left: 50%;
  transform: translateX(-50%);
  background: #B19CD9;
  color: white;
  padding: 12px 30px;
  border-radius: 25px;
  border: none;
  box-shadow: 0 4px 12px rgba(177, 156, 217, 0.3);
}

.generate-btn:disabled {
  background: #ccc;
}

.loading-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(255,255,255,0.9);
  display: none;
  justify-content: center;
  align-items: center;
  z-index: 1000;
}

.loading-content {
  text-align: center;
}

.spinner {
  margin-bottom: 1rem;
}
</style> 